<?php


namespace app\admin\controller\Feedback;


use app\admin\controller\BaseController;
use app\admin\model\Feedback\UserFeedbackTypeModel;
use app\Utils\Util;
use think\facade\Db;
use think\Request;

class UserFeedbackType extends BaseController
{

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getFeedbackTypeList(Request $request)
    {
        $param = $request->param();
        $where = [];
        $page = $param['page'];
        $limit = $param['limit'];
        $type_name = !empty($param['type_name']) ? $param['type_name'] : "";
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if (empty($page)) {
            $page = 1;
        }
        if (empty($limit)) {
            $limit = 10;
        }

        if (!empty($type_name)) {
            array_push($where, ['type_name', 'like', '%' . $type_name . '%']);
        }
        if (!empty($is_show)) {
            if ($is_show == 1) {
                array_push($where, ['is_show', '=', 1]);
            }
            if ($is_show == 2) {
                array_push($where, ['is_show', '=', 0]);
            }
        }
        $list = UserFeedbackTypeModel::getUserFeedbackTypeListWhere($page, $limit, $where);
        $count = UserFeedbackTypeModel::getUserFeedbackTypeCountWhere($where);

        return Util::returnPageJson(self::CODE_DATA, $count, $list, "");
    }

    /**
     * 添加
     * @param Request $request
     * @return array
     */
    public function addFeedbackType(Request $request)
    {
        $param = $request->param();
        $type_name = !empty($param['type_name']) ? $param['type_name'] : "";
        $sort = !empty($param['sort']) ? $param['sort'] : 0;
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        $data = [
            'type_name' => $type_name,
            'sort' => $sort,
            'add_time' => date("Y-m-d H:i:s",time())
        ];

        if (!empty($is_show) && $is_show == "on") {
            $data['is_show'] = 1;
        }
        $id = UserFeedbackTypeModel::addUserFeedbackType($data);
        if ($id) {
            return Util::returnJson(self::CODE_SUCCESS, $id, "添加成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "添加失败");
        }
    }

    /**
     * 更新
     * @param Request $request
     * @return array
     */
    public function editFeedbackType(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        $type_name = !empty($param['type_name']) ? $param['type_name'] : "";
        $sort = !empty($param['sort']) ? $param['sort'] : 0;
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if (empty($id)) {
            return Util::returnJson(self::CODE_ERROR, null, "参数错误");
        }

        if (!empty($type_name)) {
            $save['type_name'] = $type_name;
        }
        if (!empty($sort)) {
            $save['sort'] = $sort;
        }
        if (!empty($is_show)) {
            if ($is_show == "on") {
                $save['is_show'] = 1;
            } else {
                $save['is_show'] = 0;
            }
        }else{
            $save['is_show'] = 0;
        }
        $flag = UserFeedbackTypeModel::updateUserFeedbackTypeWhere([['id', '=', $id]], $save);
        if ($flag) {
            return Util::returnJson(self::CODE_SUCCESS, null, "修改成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "修改失败");
        }
    }

    /**
     * 删除
     * @param Request $request
     * @return array
     */
    public function delFeedbackType(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        //开启事务
        Db::startTrans();
        try {
            //中间逻辑代码
            $flag = UserFeedbackTypeModel::delUserFeedbackTypeWhere([['id', '=', $id]]);
            if ($flag) {
                DB::commit();
                return Util::returnJson(self::CODE_SUCCESS, null, "删除成功3");
            } else {
                DB::rollBack();
                return Util::returnJson(self::CODE_ERROR, null, "删除失败2");
            }
        } catch (\Exception $e) {
            //接收异常处理并回滚
            //回滚事务
            DB::rollBack();
            return Util::returnJson(self::CODE_ERROR, $e, "删除失败1");
        }
    }

}